<?php
namespace Kuoqi\Interpreter;

class Volist{
	static protected $assign,$code;
	static public function execCode($code,$assign){
		self::$assign = $assign;
		self::$code = $code;
		self::__initializate();
		$code = self::$code;
		self::$code = '';
		return 	$code;
	}
	static private function __initializate(){
		//获取标签头列表
		preg_match_all('/(<volist[\s\S]*?>)/',self::$code,$lis);
		//分别获取name,value,id值
		$needlist = array();
		foreach($lis[0] as $k=>$v){
			$needlist[$k]['data'] = $v;
			$namestatus = preg_match('/name="([\s\S]*?")/',$v,$lisname);
			if($namestatus){
				$needlist[$k]['lis'] = substr($lisname[1],0,strlen($lisname[1])-1);
			}else{
				$needlist[$k]['lis'] = false;
			}
			
			$valuestatus = preg_match('/value="([\s\S]*?")/',$v,$lisvalue);
			if($valuestatus){
				$needlist[$k]['val'] = substr($lisvalue[1],0,strlen($lisvalue[1])-1);
			}else{
				$needlist[$k]['val'] = 'v';
			}
			
			$idstatus = preg_match('/id="([\s\S]*?")/',$v,$lisid);
			if($idstatus){
				$needlist[$k]['key'] = substr($lisid[1],0,strlen($lisid[1])-1);
			}else{
				$needlist[$k]['key'] = 'k';
			}
		}
		
		foreach($needlist as $v){	
			self::setOnekey($v['data'],$v['lis'],$v['key'],$v['val']);
		}
		self::$code = str_replace('</volist>','<?php }}?>',self::$code);
	}
	
	static private function setOnekey($data,$lis,$key,$val){
		$repstr = '<?php if(isset($'.$lis.') && is_array($'.$lis.')){foreach($'.$lis.' as $'.$key.'=>$'.$val.'){?>';
		self::$code = str_replace($data,$repstr,self::$code);
	}
		
	static public function getData(){
		return self::$code;
	}
}